package com.skp.shaphan.io;

import java.io.IOException;
import java.io.OutputStream;

public class HtmlDataWriter extends StringBasedDataWriter {

	@Override
	protected void createStreams(OutputStream stream) throws IOException {
		super.createStreams(stream);
		println("<div width='100%' style='color: #333; font-family: Arial, Helvetica, sans-serif;'>" +
		        "<table style='background-color: transparent; border-spacing: 0; border: 1px solid #dddddd; border-collapse: separate; border-left: 0; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-raduis: 4px; margin-bottom: 20px>");
	}

	@Override
	public void close() throws IOException {
		println("</table></div>");
		super.close();
	}

	@Override
	public void finishHeadings() {
		println("</tr>");
	}

	@Override
	protected void writeHeadingCellData(String data) {
		if(!isHeadingWritten()) {
			println("<tr style='border-collapse: separate;border-spacing: 0;>");
		}
		println("<th style='vertical-align: bottom;border-left: 1px solid #dddddd;font-weight: bold;padding: 8px;line-height: 20px;text-align: left;border-top: 1px solid #dddddd;'>");
		println(stringToHTMLString(data));
		println("</th>");
	}

	// http://www.rgagnon.com/javadetails/java-0306.html
	public static String stringToHTMLString(String string) {
	    StringBuffer sb = new StringBuffer(string.length());
	    // true if last char was blank
	    boolean lastWasBlankChar = false;
	    int len = string.length();
	    char c;

	    for (int i = 0; i < len; i++)
	        {
	        c = string.charAt(i);
	        if (c == ' ') {
	            // blank gets extra work,
	            // this solves the problem you get if you replace all
	            // blanks with &nbsp;, if you do that you loss 
	            // word breaking
	            if (lastWasBlankChar) {
	                lastWasBlankChar = false;
	                sb.append("&nbsp;");
	                }
	            else {
	                lastWasBlankChar = true;
	                sb.append(' ');
	                }
	            }
	        else {
	            lastWasBlankChar = false;
	            //
	            // HTML Special Chars
	            if (c == '"')
	                sb.append("&quot;");
	            else if (c == '&')
	                sb.append("&amp;");
	            else if (c == '<')
	                sb.append("&lt;");
	            else if (c == '>')
	                sb.append("&gt;");
	            else if (c == '\n')
	                // Handle Newline
	                sb.append("&lt;br/&gt;");
	            else {
	                int ci = 0xffff & c;
	                if (ci < 160 )
	                    // nothing special only 7 Bit
	                    sb.append(c);
	                else {
	                    // Not 7 Bit use the unicode system
	                    sb.append("&#");
	                    sb.append(new Integer(ci).toString());
	                    sb.append(';');
	                    }
	                }
	            }
	        }
	    return sb.toString();
	}

	@Override
	public void closeRow() {
		println("</tr>");
		super.closeRow();
	}

	@Override
	protected void writeCellData(String data, int columnNumber) {
		println("<td style='border-left: 1px solid #dddddd;padding: 8px;line-height: 20px;text-align: left;vertical-align: top;border-top: 1px solid #dddddd;border-spacing: 0;white-space:nowrap;'>");
		println(stringToHTMLString(data));
		println("</td>");
	}

	@Override
	protected void writeNewRowData() {
		if(isOddRow()) {
			println("<tr bgcolor='#f9f9f9' style='border-spacing: 2px; border-color: gray;background-color: #f9f9f9'>");
		} else {
			println("<tr bgcolor='#FFFFFF' style='background-color:#FFFFFF'>");
		}
	}
}
